package src.leetcode.everyday.mid_2749;


/**
 * @Author: 李逸重
 * @CreateTime: 2025-09-05
 * @Description:
 * @Version: 1.0
 */
public class makeTheIntegerZero {
    public static void main(String[] args) {
        System.out.println(new Solution().makeTheIntegerZero(112577768, -501662198));
    }
}


class Solution {
    public int makeTheIntegerZero(int num1, int num2) {
        if (num1 - (num2 + 1) < 0  && num2 + 1 >= 0) {
            return -1;
        }
        int cur = 1;
        if (num1 - (num2 + 1) < 0  && num2 + 1 >= 0) {
            cur = Math.max(cur, num1 / (num2 + 1));
        }

        for (int i = cur; ; i++) {
            if (num1 - (num2 + 1) * i < 0  && num2 + 1 >= 0) {
                return -1;
            }
            int n = getBitsOne(num1 - num2 * 1L * i);
            if (n <= i) {
                return i;
            }
        }
    }

    private int getBitsOne(long num) {
        if (num < 0) {
            return Integer.MAX_VALUE;
        }

        int res = 0;
        while (num != 0) {
            res += num & 1;
            num >>= 1;
        }
        return res;
    }
}